﻿using System;
using System.Configuration;
using System.Web;

namespace BackEnd
{
	public partial class Login : System.Web.UI.Page
	{

		protected void Page_Load(object sender, EventArgs e)
		{
			if (PreviousPage != null && PreviousPage.Request.Url != null)
			{
				Session[GeneralResource.startPage] = PreviousPage.Request.Url.AbsoluteUri;
			}
		}

		protected void btnSend_Click(object sender, EventArgs e)
		{
			string login = ConfigurationManager.AppSettings[GeneralResource.nameAdmin];
			string passw = ConfigurationManager.AppSettings[GeneralResource.passwAdmin];

			if (tbxLogin.Text == login && tbxPassw.Text == passw)
			{
				Session[GeneralResource.nameCookie] = true;

				if (chbRemember.Checked)	//if user choose to remember, then save the cookie
				{
					HttpCookie cookie = new HttpCookie(GeneralResource.nameCookie);
					cookie[login.GetHashCode().ToString()] = passw.GetHashCode().ToString();
					cookie.Expires = DateTime.Now.AddMonths(1);
					Response.Cookies.Add(cookie);
				}

				if (Session[GeneralResource.startPage] != null)
				{
					//redirected to the requested page
					Response.Redirect(Session[GeneralResource.startPage] as string, true);
				}
				else
					//redirected to the requested page
					Response.Redirect(string.Format(GeneralResource.urlCatalog, 1), true);
			}
		}

	}
}